package cc.admin.modules.sys.controller;

import cc.admin.common.api.vo.Result;
import cc.admin.common.aspect.annotation.AutoLog;
import cc.admin.common.sys.base.controller.BaseController;
import cc.admin.common.sys.query.QueryGenerator;
import cc.admin.common.sys.vo.LoginUser;
import cc.admin.modules.sys.entity.SysSubsystem;
import cc.admin.modules.sys.service.ISysLicenceService;
import cc.admin.modules.sys.service.ISysSubsystemService;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * \n
 *
 * @author zxy
 * @date 2022-07-06 17:02
 * @description description\n
 */
@Slf4j
@Api(tags = "子系统管理")
@RestController
@RequestMapping("/sys/subsystem")
public class SysSubsystemController extends BaseController<SysSubsystem, ISysSubsystemService> {

    @Autowired
    private ISysSubsystemService sysSubsystemService;

    @Autowired
    private ISysLicenceService sysLicenceService;

    /**
     * 列表查询
     *
     * @param sysSubsystem
     * @param pageNo
     * @param pageSize
     * @param req
     * @return
     */
    @AutoLog(value = "子系统-分页查询列表")
    @ApiOperation(value = "子系统-分页查询列表", notes = "子系统-分页查询列表")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(SysSubsystem sysSubsystem,
                                   @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                   @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                   HttpServletRequest req) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        QueryWrapper<SysSubsystem> queryWrapper = QueryGenerator.initQueryWrapper(sysSubsystem, req.getParameterMap());
		queryWrapper.orderByAsc("sort");
//		String moduleId="";
//		if(!"1".equals(sysUser.getIsAdmin())) {
//            List<Map<String, Object>> list = sysLicenceService.getUserLicences(sysUser.getTenantId());
//            if (list.size() > 0) {
//                Map map = list.get(0);
//                moduleId = map.get("module_ids").toString();
//                if (moduleId.length() > 0) {
//                    queryWrapper.in("code", StrUtil.splitTrim(moduleId, ","));
//                }
//            }
//        }

        Page<SysSubsystem> page = new Page<SysSubsystem>(pageNo, pageSize);
        IPage<SysSubsystem> pageList = sysSubsystemService.page(page, queryWrapper);
        return Result.ok(pageList);
    }

    /**
     * 新增
     *
     * @param subsystem
     * @return
     */
    @AutoLog(value = "子系统-新增")
    @ApiOperation(value = "子系统-新增", notes = "子系统-新增")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody SysSubsystem subsystem) {
        sysSubsystemService.save(subsystem);
        return Result.ok("新增成功");
    }

    /**
     * 编辑
     *
     * @param sysSubsystem
     * @return
     */
    @AutoLog(value = "子系统-编辑")
    @ApiOperation(value = "子系统-编辑", notes = "子系统-编辑")
    @PutMapping(value = "/edit")
    public Result<?> update(@RequestBody SysSubsystem sysSubsystem) {
        sysSubsystemService.updateById(sysSubsystem);
        return Result.ok("修改成功");
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @AutoLog(value = "子系统-删除")
    @ApiOperation(value = "子系统-删除", notes = "子系统-删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        sysSubsystemService.removeById(id);
        return Result.ok("删除成功");
    }

    @AutoLog(value = "子系统-批量删除")
    @ApiOperation(value = "子系统-批量删除", notes = "子系统-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        sysSubsystemService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.ok("批量删除成功");
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @AutoLog(value = "子系统-根据id查询")
    @ApiOperation(value = "子系统-根据id查询", notes = "子系统-根据id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name = "id", required = true) String id) {
        SysSubsystem sysSubsystem = sysSubsystemService.getById(id);
        return Result.ok(sysSubsystem);
    }

    /**
     * 导出exce
     *
     * @param request
     * @param sysSubsystem
     * @return
     */
    @RequestMapping("/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SysSubsystem sysSubsystem) {
        return super.exportXls(request, sysSubsystem, SysSubsystem.class, "子系统");
    }

    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SysSubsystem.class);
    }


    /**
     * 子系统下拉
     * @return
     */
    @RequestMapping(value = "/getSubSystemName", method = RequestMethod.GET)
    public  Result<?> getSubSystemName(){
        List<Map<String, String>> dataList= sysSubsystemService.getSubSystemName();
        return Result.ok(dataList);
    }

}
